import { _decorator, Component, Node, Animation, Label } from 'cc';
import EvtManger from '../tools/EvtManger';
import { AppControll, EventName } from '../manager/AppControll';
import { DataHelper } from '../system/DataHelper';
import { StuffIcon } from './StuffIcon';
import { GameData } from '../system/GameData';
const { ccclass, property } = _decorator;
export type LevelResult = {
    level: number,
    time: number,
}

@ccclass('LevelChallengeResult')
export class LevelChallengeResult extends Component {
    private mNdBody: Node = null;
    private mNdNow: Node = null;
    private mNdWeek: Node = null;
    private mResultNow: LevelResult = null;
    private mResultMax: LevelResult = null;
    private mNdReward: Node = null;
    private firstLevel: number = 10000;

    OnClose() {
        EvtManger.do(EventName.doExitLevel);
        let config = DataHelper.GetLevelModelRefreshById(this.firstLevel + this.mResultNow.level);
        if (!config) {
            this.node.destroy();
            return;
        }
        let lt = config.levelReward;
        lt.forEach(reward => {
            let nd = this.mNdReward.getChildByName(reward[0] + "");
            GameData.GetNumByType(reward[0], reward[1]);
            let iconPath = GameData.mRes[reward[0]];
            let propConig = { propIcon: iconPath, propNum: reward[1] };
            GameData.DropByType(reward[0], propConig, nd.position.add(nd.parent.position));
        })
        this.node.destroy();
    }

    protected onLoad(): void {
        this.mNdBody = this.node.getChildByName("body");
        this.mNdNow = this.mNdBody.getChildByName("now");
        this.mNdWeek = this.mNdBody.getChildByName("week");
        this.mNdReward = this.mNdBody.getChildByName("reward");
    }

    InitData(now: LevelResult, max: LevelResult) {
        this.mResultNow = now;
        this.mResultMax = max;
        this.InitView();
    }

    InitView() {
        this.mNdNow.getChildByName("level").getComponent(Label).string = this.mResultNow.level + "";
        this.mNdNow.getChildByName("times").getComponent(Label).string = this.mResultNow.time.toFixed(2);

        this.mNdWeek.getChildByName("level").getComponent(Label).string = this.mResultMax.level + "";
        this.mNdWeek.getChildByName("times").getComponent(Label).string = this.mResultMax.time.toFixed(2);
        this.InitReward();
        this.mNdBody.getComponent(Animation).play();
    }

    InitReward() {
        let config = DataHelper.GetLevelModelRefreshById(this.firstLevel + this.mResultNow.level);
        this.mNdReward.children.forEach(nd => nd.active = false)
        if (!config) return;
        let lt = config.levelReward;
        lt.forEach(reward => {
            let nd = this.mNdReward.getChildByName(reward[0] + "");
            nd.active = true
            nd.getChildByName("num").getComponent(Label).string = reward[1];
            nd.getChildByName("icon").getComponent(StuffIcon).ShowStuff(reward[0]);
        })
    }
}


